<template>
	<view>
		<view class="upload" @tap="testFun()">
			上传图片
		</view>
		<image v-for="(item,index) in img" :src="item" mode="aspectFit" :key='index'></image>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				id:'nihao',
				user:"user",
				img:[]
			};
		},
		methods: {
			testFun: function() {
				uni.chooseImage({
					count: 9,
					sizeType: ['original', 'compressed'],
					sourceType: ['ablum', 'camera'],
					success: (chooseImageRes) => {
						const tempFilePaths = chooseImageRes.tempFilePaths;
						tempFilePaths.forEach((item, index)=> {
							const uploadTask = uni.uploadFile({
								url: 'http://192.168.43.201:3000/files/upload',
								filePath: tempFilePaths[index],
								name: 'file',
								formData: {
									'url': 'user',
									'name': new Date().getTime()+this.id+index,
								},
								success: (uploadFileRes) => {
									let data = JSON.parse(uploadFileRes.data);
									let fielname = data[0].filename;
									this.img.push('http://192.168.43.201:3000/user/'+fielname);
									// console.log(fielname);
								}
							});

							uploadTask.onProgressUpdate((res) => {
								console.log('上传进度' + res.progress);
								console.log('已经上传的数据长度' + res.totalBytesSent);
								console.log('预期需要上传的数据总长度' + res.totalBytesExpectedToSend);

								// 测试条件，取消上传任务。
								// if (res.progress > 50) {
								//     uploadTask.abort();
								// }
							});
						})

					}
				});
			}
		},
	}
</script>

<style lang="scss">
	.upload {
		padding-top: 40rpx;
		text-align: center;
	}
</style>
